<?php
/**
 * Created by PhpStorm.
 * User: 24339
 * Date: 2018/10/15
 * Time: 22:12
 */

namespace app\index\model;
use think\Model;
class Company extends Model
{
    protected static function init()
    {
        Company::event('before_update', function ($data) {
            if($_FILES['logo']['tmp_name']){
                $arts=Company::find($data->id);
                $thumbpath=$_SERVER['DOCUMENT_ROOT'].$arts['thumb'];
                if(file_exists($thumbpath)){
                    @unlink($thumbpath);
                }
                $file = request()->file('thumb');
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                    $thumb='/tp/' . 'public/' . DS . 'uploads'.'/'.$info->getSaveName();
                    $data['thumb']=$thumb;
                }

            }
        });
        Company::event('before_delete', function ($data) {

            $arts=Company::find($data->id);
            $thumbpath=$_SERVER['DOCUMENT_ROOT'].$arts['thumb'];
            if(file_exists($thumbpath)){
                @unlink($thumbpath);
            }
        });
    }
}